package com.bw.sign

import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.*
import com.baidu.mapapi.map.BaiduMap.OnMapClickListener
import com.baidu.mapapi.model.LatLng
import com.blankj.utilcode.util.SPUtils
import com.bw.lib_base.mvvm.BaseViewActivity
import com.bw.sign.databinding.ActivityMapBinding


@Route(path = "/sign/MapActivity")
class MapActivity : BaseViewActivity<ActivityMapBinding>() {
    var map: MapView ?= null
    var mbaiduMap: BaiduMap ?= null
    var mLocationClient : LocationClient ?=null
    override fun initView() {
        requestPermissions(arrayOf(
            "android.permission.ACCESS_COARSE_LOCATION",
            "android.permission.ACCESS_FINE_LOCATION",
            "android.permission.ACCESS_WIFI_STATE"),100)
        initMap()
    }

    override fun initData() {

    }

    fun initMap(){
        map = binding.map
        mbaiduMap = map?.map
        mbaiduMap?.setMyLocationEnabled(true)

        //定位初始化
        //定位初始化
        LocationClient.setAgreePrivacy(true)
        mLocationClient = LocationClient(this)

//通过LocationClientOption设置LocationClient相关参数

//通过LocationClientOption设置LocationClient相关参数
        var option = LocationClientOption()
        option.setIsNeedAddress(true)
        option.setIsNeedLocationDescribe(true)
        option.isOpenGps = true // 打开gps

        option.setCoorType("bd09ll") // 设置坐标类型

        option.setScanSpan(1000)

//设置locationClientOption

//设置locationClientOption
        mLocationClient!!.setLocOption(option)

//注册LocationListener监听器

//注册LocationListener监听器
        val myLocationListener = MyLocationListener()
        mLocationClient?.registerLocationListener(myLocationListener)
//         设置定位图标
        val locIcon = BitmapDescriptorFactory.fromResource(R.drawable.ic_location)
        val locConfiguration = MyLocationConfiguration(
            MyLocationConfiguration.LocationMode.NORMAL,
            true,  // 是否显示方向
            locIcon
        )
        mbaiduMap?.setMyLocationConfiguration(locConfiguration)
        option = LocationClientOption().apply {

            isOpenGps = true // 强制开启GPS
            setCoorType("bd09ll") // 坐标类型
        }
        mLocationClient?.locOption = option
//开启地图定位图层
//开启地图定位图层
        mLocationClient?.start()

        mbaiduMap?.setOnMapClickListener(object :OnMapClickListener{
            override fun onMapClick(p0: LatLng?) {
                Log.i("zzzzzzzzz", "${p0?.latitude}")
                SPUtils.getInstance().put("lat","${p0?.latitude},${p0?.longitude}")
                ARouter.getInstance().build("/sign/SignActivity").withString("loc","八维学院").navigation()
            }

            override fun onMapPoiClick(p0: MapPoi?) {

            }
        })
    }

    inner class MyLocationListener : BDAbstractLocationListener() {
        override fun onReceiveLocation(location: BDLocation) {
//            Log.d("Location", "收到定位: ${location.latitude}, ${location.longitude}")
            //mapView 销毁后不在处理新接收的位置
            if (location == null || map == null) {
                return
            }
            val locData = MyLocationData.Builder()
                .accuracy(location.radius) // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(location.direction).latitude(location.latitude)
                .longitude(location.longitude).build()
            val locationDescribe = location.locationDescribe //获取位置描述信息
            val addr = location.addrStr //获取详细地址信息

            Log.i("zzzzzzzzzz", "${"详细地址："+addr}" +
                    "${"位置描述："+locationDescribe}")
            mbaiduMap?.setMyLocationData(locData)
        }
    }


    override fun onDestroy() {
        mLocationClient!!.stop()
        mbaiduMap?.setMyLocationEnabled(false)
        map?.onDestroy()
        map = null
        super.onDestroy()
    }
}